package com.yonyou.pmclouds.plantemplate.processor;

import com.yonyou.pmclouds.basecom.bp.processor.SingleProcessor;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.plantemplate.entity.ScheduleTemplateVO;
import com.yonyou.pmclouds.plantemplate.rmiitf.IScheduleTemplateQueryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 * @Description: 进度计划模板保存前处理类
 * @Author: zhangzhonghao
 * @Date: 2020-03-06
 *
 */

@Component
public class ScheduleTemplateUpdateBeforeProcessor implements SingleProcessor<ScheduleTemplateVO> {

    @Autowired
    private IScheduleTemplateQueryService templateQueryService;

    @Override
    public void process(ScheduleTemplateVO templateVO, ScheduleTemplateVO orig) throws BusinessException {
        ScheduleTemplateVO result = templateQueryService.queryById(templateVO.getPkScheduleTemplate());
        String creator = RuntimeEnvironment.getLoginUserId();

        if(result == null){
            throw new BusinessException("未找到该计划，请检查数据!");
        }
        if (result.getCreator() == null || !creator.equals(result.getCreator())) {
            throw new BusinessException("当前用户不是该根计划的创建人，不可操作！");
        }

        if(StringUtils.isEmpty(templateVO.getName())){
            throw new BusinessException("计划名称不允许为空！");
        }

    }

}
